{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "71d3aa1c-c86c-4284-822c-f703fdaeb9b2",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/contains-duplicate-ii\n",
    "\n",
    "\n",
    "Runtime: 116 ms, faster than 7.14% of Python3 online submissions for Contains Duplicate II.\n",
    "Memory Usage: 28.8 MB, less than 6.27% of Python3 online submissions for Contains Duplicate II.\n",
    "\n",
    "\n",
    "```python\n",
    "from itertools import combinations\n",
    "\n",
    "class Solution:\n",
    "    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n",
    "        #8:07\n",
    "        num_dict = dict()\n",
    "        for index, n in enumerate(nums):\n",
    "            if n in num_dict.keys():\n",
    "                num_dict[n].append((index, n))\n",
    "            else:\n",
    "                num_dict[n] = [(index, n)]\n",
    "        for key, value in num_dict.items():\n",
    "            if len(value) >= 2:\n",
    "                #print(key, value)\n",
    "                for combination in combinations(value, 2):\n",
    "                    a = combination[0][0]\n",
    "                    b = combination[1][0]\n",
    "                    if abs(a-b) <= k:\n",
    "                        return True\n",
    "        return False\n",
    "        #8:14\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24fa0ae0-bfdc-40bb-8933-88bdcb586d79",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
